package com.cloudkinto.controller.platform;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.platform.sync.PlatformOrderSyncRecordItemService;
import com.cloudkinto.service.platform.sync.vo.PlatformOrderSyncRecordRes;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.platform.sync.PlatformOrderSyncRecordService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2025-06-17
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "平台订单同步记录模块")
@RequestMapping("/api/admin/oms/PlatformOrderSyncRecord")
public class PlatformOrderSyncRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(PlatformOrderSyncRecordController.class);

    private final PlatformOrderSyncRecordService service;
    private final PlatformOrderSyncRecordItemService platformOrderSyncRecordItemService;




    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id,userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id));
    }

    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }


    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺id"),
            @ApiImplicitParam(name = "platformCode", value = "平台"),
            @ApiImplicitParam(name = "syncStatus", value = "同步状态"),
            @ApiImplicitParam(name = "beginTime", value = "创建时间"),
            @ApiImplicitParam(name = "endTime", value = "创建时间"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = PlatformOrderSyncRecordRes.class))
    public PageResult listPage(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        if (companyId != null) {
            map.put("companyId", companyId.toString());
        }
        String storeId = request.getParameter("storeId");
        map.put("storeId", storeId);
        String platformCode = request.getParameter("platformCode");
        map.put("platformCode", platformCode);
        String syncStatus = request.getParameter("syncStatus");
        map.put("syncStatus", syncStatus);
        String beginTime = request.getParameter("beginTime");
        map.put("beginTime", beginTime);
        String endTime = request.getParameter("endTime");
        map.put("endTime", endTime);
        return service.getListPage(map);
    }



    @UserSessionRequired
    @GetMapping("/itemListPage")
    @ApiOperation(value = "获取同步订单单号详情（分页）", notes = "获取同步订单单号详情（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "syncId", value = "同步记录id"),
            @ApiImplicitParam(name = "platformOrderNo", value = "单号(精准查询)"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功", response = PlatformOrderSyncRecordRes.class))
    public PageResult itemListPage(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        if (companyId != null) {
            map.put("companyId", companyId.toString());
        }
        String syncId = request.getParameter("syncId");
        map.put("syncId", syncId);
        String platformOrderNo = request.getParameter("platformOrderNo");
        map.put("platformOrderNo", platformOrderNo);

        return platformOrderSyncRecordItemService.getListPage(map);
    }
}

